﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using Glue.Lib;

namespace SqueezeboxBridge
{
    static class Program
    {
        private static string _server;
        private static string _player;
        private static int _playerId;
        private static bool _addToPlaylist = false;
        private static bool _showUI = true;
        private static bool _showHelp = false;
        private static List<string> _playlist = new List<string>();

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main(string[] pars)
        {
            ProcessCommandLine(pars);

            // 
            if (_showUI)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1(pars));
            }
        }

        private static void ProcessCommandLine(string[] pars)
        {
            // command line processing
            //try
            {
                for (int i = 0; i < pars.Length; i++)
                {
                    switch (pars[i].ToLower())
                    {
                        case "/a":
                        case "-a":
                            _addToPlaylist = true;
                            _showUI = false;
                            break;

                        case "/p":
                        case "-p":
                            i++;
                            _player = pars[i];
                            _showUI = false;
                            break;

                        case "/i":
                        case "-i":
                            i++;
                            _playerId = Int32.Parse(pars[i]);
                            _showUI = false;
                            break;

                        case "/s":
                        case "-s":
                            i++;
                            _server = pars[i];
                            _showUI = false;
                            break;

                        case "/help":
                        case "--help":
                        case "/h":
                        case "-h":
                        case "/?":
                        case "-?":
                            _showHelp = true;
                            _showUI = false;
                            break;

                        case "/startserver":
                        case "--startserver":
                            StartStreamServer();

                            _showUI = false;
                            break;

                        case "/startserverinternal":
                        case "--startserverinternal":
                            StartStreamServerInternal();
                            _showUI = false;
                            break;

                        case "/stopserver":
                        case "--stopserver":
                            StopStreamServer();
                            _showUI = false;
                            break;

                        default:
                            _playlist.Add(pars[i]);
                            _showUI = false;
                            break;
                    }

                }
            }
            //catch (Exception e)
            //{
            //    Log.Error(e);
            //    
            //}
            if (_showHelp)
                DisplayHelp();
        }

        private static void StartStreamServer()
        {
            // Create new proces for stream server
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "squeezeboxbridge.exe";
            process.StartInfo.Arguments = "/startserverinternal";

            // hide console window
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;

            // start
            process.Start();
        }

        private static void StartStreamServerInternal()
        {
            StreamServer.Start();
        }

        private static void StopStreamServer()
        {
            StreamServer.Stop();
        }

        private static void DisplayHelp()
        {
            string help =
                "SqueezeboxBridge v0.1 - (C) 2010 Grondwerk.it\n"
                + "\nUsage:          \n\n  squeezeboxbridge [OPTIONS] [audio file] [audio file] [...]\n"
                
                + "\nOptions:\n"
                
                + "\n  /a              Add file(s) to playlist instead of playing them directly"
                + "\n  /p PLAYER       Specify Squeezebox friendly name, i.e. `Kitchen_squeeze'"
                + "\n  /i PLAYER_ID    Specify Squeezebox player id, i.e. 0, 1, 2"
                + "\n  /s SERVER       Specify Squeezebox server name, i.e. `slimserver',"
                + "\n                  or `192.168.0.10'"
                + "\n  /startserver    Start SqueezeboxBridge stream server manually"
                + "\n  /stopserver     Stop SqueezeboxBridge stream server manually"
                + "\n  /?, /h, /help   Display this help message\n"

                + "\nExamples:\n"
                + "\n  squeezeboxbridge my_favorite_track.mp3"
                + "\n  squeezeboxbridge /a my_favorite_track.mp3"
                + "\n  squeezeboxbridge /p kitchen_squeeze /s slimserver my_favorite_track.mp3\n"
                
                + "\nRemarks:\n"
                + "\nPlayer, player_id and server-parameters are optional. By default, the last "
                + "\nvalues are used. If the application starts for the first time, the  "
                + "\nconfiguration dialog is shown.\n"
                
                + "\nThere is no need to start or stop the stream server manually, since this is "
                + "\ndone automatically when the first file is streamed to the Squeezebox. This "
                + "\noption is for Debug-purposes only.";

                
            // running on Linux?
            int p = (int) Environment.OSVersion.Platform;
            if ((p == 4) || (p == 6) || (p == 128))
            {
                // quick_and_dirty
                help.Replace('/', '-');
                help.Replace("-help", "--help");
            }

            Console.WriteLine(help);
        }
    }
}
